package com.zjw.zy.coreui.view

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.zjw.zy.R
import com.zjw.zy.base.BaseViewGroup
import com.zjw.zy.ext.byHint

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/12/14 10:52
 */
class SettingItemView : BaseViewGroup {

    private val titleView: TextView by lazy { findViewById(R.id.setting_item_title) }
    private val imageView: ImageView by lazy { findViewById(R.id.setting_item_image) }
    private val slideBtn: SlideButton by lazy { findViewById(R.id.setting_item_slide_btn) }
    private val naviBtn: ImageView by lazy { findViewById(R.id.setting_item_nav_btn) }
    private val prevView: TextView by lazy { findViewById(R.id.setting_item_preview) }


    private var mListener: Listener? = null

    //0代表滑动模式，1代表导航模式
    private var type = 0
    private lateinit var title: String
    private var image: Drawable? = null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    override fun onLayoutId(): Int {
        return R.layout.setting_item_view
    }

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        if (slideBtn.byHint(ev)) {
            return false
        }
        return true
    }

    override fun onAttrs(context: Context?, attrs: AttributeSet?) {
        context?.let {
            it.obtainStyledAttributes(attrs, R.styleable.SettingItemView).apply {
                type = this.getInt(R.styleable.SettingItemView_type, 0)
                title = this.getString(R.styleable.SettingItemView_title) ?: "未设置"
                image = this.getDrawable(R.styleable.SettingItemView_image)
                this.recycle()
            }
        }
    }


    override fun onBindListener() {
        slideBtn.bindListener(object : SlideButton.Listener {
            override fun onEnable(enable: Boolean) {
                mListener?.onEnable(enable)
            }
        })

        this.setOnClickListener {
            mListener?.onClick()
        }

    }

    override fun onBindValue() {
        titleView.text = title
        image?.let { imageView.background = it }

        if (type == 0) {
            slideBtn.visibility = View.VISIBLE
            naviBtn.visibility = View.GONE
        } else {
            slideBtn.visibility = View.GONE
            naviBtn.visibility = View.VISIBLE
        }
    }

    override fun onFindView(rootView: View) {}


    fun bindListener(listener: Listener) {
        mListener = listener
    }

    fun setImageFromBitMap(bitmap: Bitmap) {
        imageView.background = BitmapDrawable(resources, bitmap)
    }

    fun setPrevText(prev: String) {
        this.prevView.visibility = View.VISIBLE
        this.prevView.text = prev
    }

    fun makeEnable(enable: Boolean) {
        this.slideBtn.makeEnable(enable, false)
    }

    fun getPreview(): String {
        return this.prevView.text.toString()
    }

    interface Listener {

        fun onClick() {}


        fun onEnable(enable: Boolean) {}
    }

}